const koa = require('koa');
const serve = require('koa-static');
// koa2-connect-history-api-fallback #专门处理history模式的中间件
const { historyApiFallback } = require('koa2-connect-history-api-fallback');
const path = require('path');
// koa2-proxy-middleware 跨域代理中间件
const proxy = require('koa2-proxy-middleware');
const app = new koa();

// 配置跨越代理
app.use(proxy({
  targets: {
    // (.*) means anything
    '/prod-api/(.*)': {
      // localhost:3333/prod-api/login =====>>>> http://ihrm-java.itheima.net/prod-api/login
        target: 'http://ihrm-java.itheima.net/api', //后端服务器地址 需加上api
        changeOrigin: true,
        pathRewrite: { 	
          // localhost:3333/prod-api/login ====>>>> http://ihrm-java.itheima.net/api/login
            '/prod-api': ""
        }
    }
  }
}))

// 这句话 的意思是除接口之外所有的请求都发送给了 index.html
// 这里的whiteList是 白名单的意思
app.use(historyApiFallback({ 
  whiteList: ['/prod-api']
}));  

app.use(serve(__dirname + '/public'));

app.listen(3333, () => {
  console.log('人资项目启动');
})
